home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / code / p_serlib.sit / Serial Library Source Code / interpret.c < prev    next >
Text File  |  1989-07-27  |  3KB  |  110 lines

  1. /***********************************************************************/
  2. /*    
  3. /*    interpret.c
  4. /*    by Atul Butte
  5. /*    Copyright ⌐ 1989 by Microsoft Corporation
  6. /*    All Rights Reserved
  7. /*
  8. /*    version 1.0
  9. /*    
  10. /*    
  11. /*    This module provides a routine which will interpret escape
  12. /*    characters in place.
  13. /*    
  14. /***********************************************************************/
  15.  
  16. /***********************************************************************/
  17. /*
  18. /*    interpret
  19. /*
  20. /***********************************************************************/
  21.  
  22. OSErr interpret( pst )
  23.     
  24.     register char                        *pst;
  25.     
  26. {
  27.     register unsigned char                cchSrc = *pst;
  28.     register unsigned char                cchDest = 0;
  29.     register char                        *pstSrc = pst;
  30.     register char                        *pstDest = pst;
  31.     register char                        ch;
  32.     
  33.     pstSrc++;
  34.     pstDest = pstSrc;
  35.     for( ; cchSrc > 0; cchSrc-- ) {
  36.         if( *pstSrc == '\\' ) {
  37.             if( cchSrc <= 1 ) {
  38.                 display_error( "Not enough characters in string for \\ command." );
  39.                 return( errStrFormat );
  40.             } else {
  41.                 pstSrc++;
  42.                 cchSrc--;
  43.                 ch = *pstSrc;
  44.                 switch( ch ) {
  45.                     case 'n':
  46.                     case 'r':
  47.                         *pstDest = kchReturn;
  48.                         break;
  49.                     case 'b':
  50.                         *pstDest = kchBackspace;
  51.                         break;
  52.                     case 'l':
  53.                         *pstDest = kchLinefeed;
  54.                         break;
  55.                     case '\\':
  56.                         *pstDest = '\\';
  57.                         break;
  58.                     case 'a':
  59.                         *pstDest = kchBell;
  60.                         break;
  61.                     case 'x':
  62.                         if( cchSrc <= 2 ) {
  63.                             display_error( "Not enough digits in string for \\x." );
  64.                             return( errStrFormat );
  65.                         } else {
  66.                             pstSrc++;
  67.                             cchSrc--;
  68.                             ch = *pstSrc;
  69.                             if( (ch >= '0') && (ch <= '9') ) {
  70.                                 *pstDest = ch - '0';
  71.                             } else if( (ch >= 'A') && (ch <= 'F') ) {
  72.                                 *pstDest = ch - 'A' + 10;
  73.                             } else if( (ch >= 'a') && (ch <= 'f') ) {
  74.                                 *pstDest = ch - 'a' + 10;
  75.                             } else {
  76.                                 display_error( "Invalid hex digits for \\x." );
  77.                                 return( errStrFormat );
  78.                             }
  79.                             pstSrc++;
  80.                             cchSrc--;
  81.                             *pstDest *= 16;
  82.                             ch = *pstSrc;
  83.                             if( (ch >= '0') && (ch <= '9') ) {
  84.                                 *pstDest += ch - '0';
  85.                             } else if( (ch >= 'A') && (ch <= 'F') ) {
  86.                                 *pstDest += ch - 'A' + 10;
  87.                             } else if( (ch >= 'a') && (ch <= 'f') ) {
  88.                                 *pstDest += ch - 'a' + 10;
  89.                             } else {
  90.                                 display_error( "Invalid hex digits for \\x." );
  91.                                 return( errStrFormat );
  92.                             }
  93.                         } /* digits for \x */
  94.                         break;
  95.                     default:
  96.                         display_error( "Invalid character after \\." );
  97.                         return( errStrFormat );
  98.                 }                    /* case */
  99.             }                        /* enough chars for \ */
  100.         } else {                    /* char is not a \ */
  101.             *pstDest = *pstSrc;
  102.         }
  103.         pstDest++;
  104.         pstSrc++;
  105.         cchDest++;
  106.     }                                /* repeat for all chars */
  107.     *pst = cchDest;
  108.     return( noErr );
  109. }
  110.